package com.hk.commons.http;

import org.apache.hc.client5.http.ClientProtocolException;
import org.apache.hc.client5.http.impl.classic.BasicHttpClientResponseHandler;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author Kevin
 */
public class UTF8HttpClientResponseHandler extends BasicHttpClientResponseHandler {

    private static final UTF8HttpClientResponseHandler responseHandler = new UTF8HttpClientResponseHandler();

    private UTF8HttpClientResponseHandler() {
    }

    public static UTF8HttpClientResponseHandler getInstance() {
        return responseHandler;
    }

    @Override
    public String handleEntity(HttpEntity entity) throws IOException {
        try {
            return EntityUtils.toString(entity, StandardCharsets.UTF_8);
        } catch (ParseException e) {
            throw new ClientProtocolException(e);
        }
    }
}
